package com.example.administrator.x2picturetransliteration.Home.Activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseActivity;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Bean.CheckBean;
import com.example.administrator.x2picturetransliteration.Home.Adapter.ToUpdateAdapter;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Activity.ToUpdatePresenter;
import com.example.administrator.x2picturetransliteration.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 检查更新
 * Created by Administrator on 2019/8/27.
 */

public class ToUpdateActivity extends BaseActivity{

    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.LButton)ImageView LButton;

    @BindView(R.id.listview)ListView listview;

    private ToUpdatePresenter mToUpdatePresenter;

    private List<CheckBean> mData;
    private ToUpdateAdapter mToUpdateAdapter;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_to_update;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TitleText.setText("检查更新");
        LButton.setImageResource(R.mipmap.home_backw);

        mToUpdatePresenter = new ToUpdatePresenter(this,mCompositeSubscriptions);

        mData = new ArrayList<>();
        mToUpdateAdapter = new ToUpdateAdapter(this,mData);


        listview.setAdapter(mToUpdateAdapter);

        mToUpdatePresenter.Check(packageName(), new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                mData.addAll((List<CheckBean>)data);
                mToUpdateAdapter.notifyDataSetChanged();

            }
        });

    }

    @OnClick(R.id.left_button)
    public void left_buttonClick(){

        finish();
    }

    private CheckBean packageName() {

        PackageManager manager = getPackageManager();

        CheckBean mCheckBean = new CheckBean();

        try {
            PackageInfo info = manager.getPackageInfo(getPackageName(), 0);

            mCheckBean.setAppVersion(info.versionName);
            mCheckBean.setInnerVersion(info.versionCode);

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return mCheckBean;
    }


}
